package com.example.playbox

import android.os.Bundle
import android.view.inputmethod.InputMethodManager
import android.widget.ListView
import android.widget.SearchView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.playbox.bean.Game
import com.example.playbox.utils.MySQLiteOpenHelper

class QueryGamesActivity : AppCompatActivity(), CustomAdapter.OnGameReservationListener {
    private var listView: ListView? = null
    private lateinit var searchView: SearchView
    private lateinit var adapter: CustomAdapter
    private var gameList: MutableList<Map<String, Any>> = mutableListOf()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_query_games)
        title = "查看记录"
        initView()
    }

    private fun initView() {
        val dao = MySQLiteOpenHelper(applicationContext)
        dao.open()
        gameList = dao.getAllGames() ?: arrayListOf()
        listView = findViewById(R.id.lst_orders)
        searchView = findViewById(R.id.searchView)
        adapter = CustomAdapter(this, gameList, this)
        listView?.adapter = adapter
        dao.close()

        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                filter(newText)
                return true
            }
        })

        searchView.setOnClickListener {
            searchView.isIconified = false
            searchView.requestFocus()
            val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
            imm.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT)
        }

        searchView.setOnQueryTextFocusChangeListener { _, hasFocus ->
            if (hasFocus) {
                val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                imm.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT)
            }
        }
    }

    private fun filter(query: String?) {
        val filteredList = if (query.isNullOrEmpty()) {
            gameList
        } else {
            gameList.filter {
                (it["gameName"] as String).contains(query, ignoreCase = true)
            }.toMutableList()
        }
        adapter.updateData(filteredList)
    }

    override fun onGameReservationUpdated(game: Game, reserved: Boolean, callback: (Boolean) -> Unit) {
        val dao = MySQLiteOpenHelper(applicationContext)
        dao.open()
        try {
            val result = dao.updateGames(game)
            if (result > 0) {
                runOnUiThread {
                    Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show()
                }
                callback(true)
            } else {
                runOnUiThread {
                    Toast.makeText(this, "更新失败", Toast.LENGTH_SHORT).show()
                }
                callback(false)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            callback(false)
        } finally {
            dao.close()
        }
    }
}